package com.scitc.pleasurevideo.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;

import com.scitc.pleasurevideo.data.DataUtil;
import com.scitc.pleasurevideo.imgutil.ImageManager;
import com.scitc.pleasurevideo.pojo.DoubanTvVo;
import com.scitc.pleasurevideo.util.OnClickUtil;

import java.util.List;


import com.scitc.pleasurevideo.R;

public class HomeGridViewAdapter extends ArrayAdapter<List<DoubanTvVo>> {
    private Context mcontext;
    private List<DoubanTvVo> items;


    public HomeGridViewAdapter(@NonNull Context context, @NonNull List<DoubanTvVo> objects) {
        super(context, 0);
        this.items = objects;
        this.mcontext = context;
    }


    public void addItems(List<DoubanTvVo> items) {
        if (items == null || items.size() < 1) {
            return;
        }
        this.items.addAll(items);
        notifyDataSetChanged();
    }

    public void resetItems(List<DoubanTvVo> items) {
        if (items == null || items.size() < 1) {
            return;
        }
        this.items.clear();
        this.items.addAll(items);
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        if (items == null || items.size() < 1) {
            return 0;
        }
        return items.size();
    }


    public DoubanTvVo getItemPosiion(int position) {
        if (position < 0 || position >= items.size()) {
            return null;
        }
        return items.get(position);

    }

    public List<DoubanTvVo> getItem() {
        if (items == null || items.size() < 1) {
            return null;
        }
        return items;
    }

    @Override
    public int getItemViewType(int position) {
        if (position < 0 || position >= items.size()) {
            return -1;
        } else {
            return 0;
        }
    }

    @Override
    public int getViewTypeCount() {
        return 2;
    }

    private View getCreateView(int position, ViewGroup parent) {
        View view;
        LayoutInflater layoutInflater = LayoutInflater.from(mcontext);
        view = layoutInflater.inflate(R.layout.item_tv_refresh_vertical, parent, false);
        return view;
    }

    class ViewHolder {
        ImageView imageView;
        ImageView[] pentagram = new ImageView[5];
        TextView titleText, directorsText, castsText, rateText;
    }

    @NonNull
    @Override
    public View getView(final int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        DoubanTvVo news = getItemPosiion(position);

        convertView = getCreateView(position, parent);
        ViewHolder holder = new ViewHolder();

        holder.imageView = convertView.findViewById(R.id.imageView);
        holder.pentagram[0] = convertView.findViewById(R.id.pentagram1);
        holder.pentagram[1] = convertView.findViewById(R.id.pentagram2);
        holder.pentagram[2] = convertView.findViewById(R.id.pentagram3);
        holder.pentagram[3] = convertView.findViewById(R.id.pentagram4);
        holder.pentagram[4] = convertView.findViewById(R.id.pentagram5);
        holder.titleText = convertView.findViewById(R.id.titleText);
        holder.directorsText = convertView.findViewById(R.id.directorsText);
        holder.castsText = convertView.findViewById(R.id.castsText);
        holder.rateText = convertView.findViewById(R.id.rateText);

        if (news != null) {
            if (holder.titleText != null) {
                holder.titleText.setText(news.getTitle());
            }

            if (holder.directorsText != null) {
                holder.directorsText.setText("导演:" + DataUtil.list_string(news.getDirectors()));
            }

            if (holder.castsText != null) {
                holder.castsText.setText("主演:" + DataUtil.list_string(news.getCasts()));
            }

            float rate_float;
            try {
                rate_float = Float.valueOf(news.getRate());
            } catch (NumberFormatException e) {
                rate_float = 0;
            }
            int rate_int = new Float(rate_float + 0.5).intValue();

            for (int i = 0; i < rate_int / 2; i++) {
                holder.pentagram[i].setImageDrawable(ContextCompat.getDrawable(mcontext, R.mipmap.pentagram_all));
            }

            if (rate_int % 2 > 0) {
                holder.pentagram[rate_int / 2].setImageDrawable(ContextCompat.getDrawable(mcontext, R.mipmap.pentagram_half));
            }

            if (holder.imageView != null) {

                ImageManager.bindRoundedCornersImageCenterCrop(mcontext, holder.imageView, news.getCover(), 4);

            }

            holder.rateText.setText(news.getRate());
        }
        convertView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                OnClickUtil.setViewClickTimes(view);
                onClickItem.onClickItem(position);
            }
        });

        return convertView;
    }

    private OnClickItem onClickItem;

    public interface OnClickItem {
        void onClickItem(int position);
    }

    public void setOnClickItem(OnClickItem onClickItem) {
        this.onClickItem = onClickItem;
    }
}
